package com.example.myapplication3.splash;

import android.animation.ArgbEvaluator;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.TextView;

import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplication3.R;
import com.example.myapplication3.splash.adapter.SplashActivityAdapter;
import com.example.myapplication3.base.BaseActivity;
import com.example.myapplication3.home.HomeActivity;

import me.relex.circleindicator.CircleIndicator3;

public class SplashActivity extends BaseActivity {
    private View main;
    private TextView textView;
    private SplashActivityAdapter adapter;
    private ViewPager2 viewPager2;
    private CircleIndicator3 indicator; // 图标指示器(第三方控件)
    @Override
    public int getLayoutMain() {
        return R.layout.activity_splash;
    }

    @Override
    public void initData() {
        SharedPreferences sp=getSharedPreferences("splash_sp", Context.MODE_PRIVATE);
        sp.edit().putBoolean("FirstLaunch", false).commit();
    }

    @Override
    public void initView() {
        // 引导页数据
        textView = findViewById(R.id.textview);
        viewPager2=findViewById(R.id.viewpager2);
        main=findViewById(R.id.main);
        indicator=findViewById(R.id.circleindicator);


        adapter =new SplashActivityAdapter(this);
        viewPager2.setAdapter(adapter);
        // 注意：先设置ViewPager，后设置指示器
        indicator.createIndicators(3, 0);
        indicator.setViewPager(viewPager2);
    }

    @Override
    public void initListener() {
        textView.setOnClickListener(v -> {
            Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
            startActivity(intent);
            finish();
        });
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            ArgbEvaluator argbEvaluator = new ArgbEvaluator();
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                if (position == 0) {
                    Object sColor = getResources().getColor(R.color.page3);
                    Object eColor = getResources().getColor(R.color.page1);
                    int bg = (int) argbEvaluator.evaluate(positionOffset, sColor, eColor);
                    main.setBackgroundColor(bg);
                }
                if (position == 1) {
                    Object sColor = getResources().getColor(R.color.page1);
                    Object eColor = getResources().getColor(R.color.page3);
                    int bg1 = (int) argbEvaluator.evaluate(positionOffset, sColor, eColor);
                    main.setBackgroundColor(bg1);
                }
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

            }
        });
            }
}